package jjn.offer_round2;

import java.util.Objects;
import java.util.Stack;

/**
 * @author Jiang Jining
 * @since 2023-07-09 8:59
 */
public class Offer31 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int index = 0;
        for (int num : pushed) {
            stack.push(num);
            while (!stack.isEmpty() && Objects.equals(stack.peek(), popped[index])) {
                stack.pop();
                index++;
            }
        }
        return stack.isEmpty();
    }
    
    public static void main(String[] args) {
        int[] pushed = new int[]{1, 2, 3, 4, 5};
        int[] poped = new int[]{4, 5, 3, 2, 1};
        System.out.println("new Offer31().validateStackSequences(pushed, poped) = " + new Offer31().validateStackSequences(pushed, poped));
    }
}
